package cn.cangfei.massage.service.impl;

import cn.cangfei.massage.constant.RedisConstants;
import cn.cangfei.massage.entity.PowerConfig;
import cn.cangfei.massage.mapper.PowerConfigMapper;
import cn.cangfei.massage.service.IPowerConfigService;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service
public class PowerConfigServiceImpl extends ServiceImpl<PowerConfigMapper, PowerConfig> implements IPowerConfigService {

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 修改配置
     *
     * @param id
     * @param show
     * @param count
     * @param url
     * @param time
     * @param action
     * @param name
     * @return
     */
    @Override
    public boolean updateConfig(Long id, String show, Integer count, String url, String time, Integer action, String name) {
        LambdaUpdateWrapper<PowerConfig> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(PowerConfig::getId, id);
        lambdaUpdateWrapper.set(PowerConfig::getIsShow, show);
        lambdaUpdateWrapper.set(PowerConfig::getCount, count);
        lambdaUpdateWrapper.set(PowerConfig::getUrl, url);
        lambdaUpdateWrapper.set(PowerConfig::getTime, time);
        lambdaUpdateWrapper.set(PowerConfig::getAction, action);
        lambdaUpdateWrapper.set(PowerConfig::getName, name);
        // 删除redis缓存
        stringRedisTemplate.delete(RedisConstants.POWER_CONFIG_PREFIX);
        boolean update = update(lambdaUpdateWrapper);
        return update;
    }

    /**
     * 查询配置信息
     *
     * @return
     */
    @Override
    public PowerConfig getConfigInfo() {
        List<PowerConfig> list = list();
        PowerConfig powerConfig = list.get(0);
        return powerConfig;
    }

    /**
     * 查询配置信息
     *
     * @param id
     * @return
     */
    @Override
    public PowerConfig getConfigInfo(Long id) {
        PowerConfig powerConfig = getById(id);
        return powerConfig;
    }
}
